﻿@{
    ViewData["Title"] = "Role";
    Layout = null;
}

<script src="~/ztreejs/jquery-3.3.1.js"></script>
<script src="~/ztreejs/jquery.ztree.core.js"></script>
<script src="~/ztreejs/jquery.ztree.excheck.js"></script>
<script src="~/ztreejs/jquery.ztree.exedit.js"></script>
<script src="~/ztreejs/jquery.ztree.exhide.js"></script>
<link href="~/zTree/bootstrap.css" rel="stylesheet" />
<link href="~/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
<script src="~/layer/layer.js"></script>
<link href="~/layer/skin/layer.css" rel="stylesheet" />
<script>

    var id = @ViewBag.id;

    //节点配置信息
      var setting = {
        view: {
            selectedMulti: true,
            showLine: true
        },
        callback: {
            onClick: zTreeOnClick
        },
        treeNode: {
            checked: true
        },
        check: {
            enable: true, //true 、 false 分别表示 显示 、不显示 复选框或单选框
           chkStyle: 'checkbox',
            chkboxType: { "Y": "ps", "N": "s" }
        },
        key: {
            name: "name",  //zTree 节点数据保存节点名称的属性名称  默认值："name"
        },
        //data: {
        //    //显示树节点的层级关系，非递归数据需要自己设置。
        //    simpleData: {
        //        enable: true,
        //        idKey: "id",   //节点数据中保存唯一标识的属性名称
        //        pIdKey: "pid",    //节点数据中保存其父节点唯一标识的属性名称
        //    },
        //    key: {
        //        url: "xxxxUrl" //不想实现点击节点实现跳转就将url设置成不存在的属性
        //    }
        //}
    };


     //获取点击节点的信息
    function zTreeOnClick(event, treeId, treeNode) {
        $("#trees").val(treeNode.id);
    };

    $(function () {
        //获取访问方法获取节点数据
        GetPower();
        SetNodeChecked();
    });


    function GetPower()
    {
        $.ajax({
            url:"/Operator/GetAllPower",
            type:"get",
            dataType:"json",
            async:false,
            success:function(obj)
            {
                console.info(obj);
                 //初始化树节点
                $.fn.zTree.init($("#treeDemo"), setting, obj);
                var treeobj = $.fn.zTree.getZTreeObj("treeDemo");
                treeobj.expandAll(true);

            }
        })
    }

    //获取所有选中节点的值
    function GetCheckedVal() {
        var msg = "";
        var ids = [];
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes(true);
        for (var i = 0; i < nodes.length; i++) {
             msg += nodes[i].name + "--" + nodes[i].id + "--" + nodes[i].pId + " <br/>";
          //  msg += nodes[i].id + ",";
          ids.push(nodes[i].id);
        }
        $.ajax({
            url:"/Operator/PostRolePower?id="+id+"&ids="+ids.toString(),
            type:'get',
            dataType:"json",
            success:function(obj)
            {
                if(obj>0)
                {
                      layer.alert('添加成功！',function(){
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                       });
                }
                else
                {
                     layer.alert('添加失败！');
                }
            }
        })
    }

    //反填
    function SetNodeChecked() {
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");

        $.ajax({
            url:"/Operator/GetRolePower?id="+id,
            type:"get",
            dataType:"json",
            success:function(obj)
            {
                $(obj).each(function(){
                     var node = treeObj.getNodeByParam("id",this.roleId);
                    treeObj.checkNode(node, true);
                })
            }
        })
    }

</script>

<div class="content_wrap">
    <div class="zTreeDemoBackground left">
        <ul id="treeDemo" class="ztree"></ul>
    </div>
    <button onclick="GetCheckedVal()" class="layui-btn layui-btn-xs">分配角色</button>
</div>

